﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace WellDone
{
    public sealed partial class SplitCheckDialog : Page
    {
        public event EventHandler CloseRequested;
        public bool bCanceled = false;
        public bool bSplitEvenly = false;
        public int iSplitCount = 0;

        public SplitCheckDialog()
        {
            this.InitializeComponent();
        }

        private void setSplitCount()
        {
            try
            {
                if (tbSplit.Text.Trim().Length > 0)
                    iSplitCount = Convert.ToInt16(tbSplit.Text.Trim());
                else
                    iSplitCount = 0;
            }
            catch (System.FormatException)
            {
                iSplitCount = 1;
            }
            // Ensure splits are between 1 and Globals.k_int_max_splits
            iSplitCount = (iSplitCount < 1 ? 1 : (iSplitCount > Globals.k_int_max_splits) ? Globals.k_int_max_splits : iSplitCount);
        }
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Set values
            setSplitCount();
            bSplitEvenly=(rbEvenly.IsChecked==true);
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // Set "Canceled" 
            bCanceled = true;
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void tbSplit_TextChanged(object sender, TextChangedEventArgs e)
        {
            setSplitCount();
            tbSplit.Text = iSplitCount.ToString();
        }

        private void btnTableDown_Click(object sender, RoutedEventArgs e)
        {
            setSplitCount();
            if (iSplitCount > 1) iSplitCount--;
            tbSplit.Text = iSplitCount.ToString();
        }

        private void btnTableUp_Click(object sender, RoutedEventArgs e)
        {
            setSplitCount();
            if ((iSplitCount >= 1)&&(iSplitCount<Globals.k_int_max_splits)) iSplitCount++;
            tbSplit.Text = iSplitCount.ToString();
        }
    }
}
